import gtk
from iabv import iabv
from sugar.activity import activity
from sugar.graphics.toolbutton import ToolButton

class iabvActivity(activity.Activity):
    def __init__(self, handle):
        activity.Activity.__init__(self, handle)
        
        self.set_title("Internet Archive Book Viewer")

        # The Sugar toolbox...
        toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(toolbox)
        
        activity_toolbar = toolbox.get_activity_toolbar()
        # Hides the "Share with:" combo-box
        activity_toolbar.share.props.visible = False
        # Hides the keep button
        activity_toolbar.keep.props.visible = False
        toolbox.show()

        #
        # Import the book reader class
        self.iabv = iabv()

        #
        # Custom reader toolbar
        #

        self.reader_toolbar = gtk.Toolbar()
        # Toolbar buttons

        # Load / Open (show/hide sidebar)
        self.load_button = ToolButton('media-eject')
        # Select icon (looks in /usr/share/icons/sugar/...
        self.load_button.set_tooltip('Load a book...')
        self.load_button.connect('clicked', self.iabv.hidepaned)
        self.reader_toolbar.insert(self.load_button, -1)
        self.load_button.show()

        # Prev page button
        self.prev_button = ToolButton('go-previous')
        # Select icon (looks in /usr/share/icons/sugar/... 
        self.prev_button.set_tooltip('Previous page')
        self.prev_button.connect('clicked', self.iabv.prev_page)
        self.reader_toolbar.insert(self.prev_button, -1)
        self.prev_button.show()

        # Next page button
        self.next_button = ToolButton('go-next') 
        # Select icon (looks in /usr/share/icons/sugar/...
        self.next_button.set_tooltip('Next page')
        self.next_button.connect('clicked', self.iabv.next_page)
        self.reader_toolbar.insert(self.next_button, -1)
        self.next_button.show()
       
        # Increase font size
        self.zoom_in_button = ToolButton('zoom-in')
        self.zoom_in_button.set_tooltip('Increase font size')
        self.zoom_in_button.connect('clicked', self.iabv.zoom_in)
        self.reader_toolbar.insert(self.zoom_in_button, -1)
        self.zoom_in_button.show()

        # Decrease font size
        self.zoom_out_button  = ToolButton('zoom-out')
        self.zoom_out_button.set_tooltip('Increase font size')
        self.zoom_out_button.connect('clicked', self.iabv.zoom_out)
        self.reader_toolbar.insert(self.zoom_out_button, -1)
        self.zoom_out_button.show()

        # Show the reader toolbar
        self.reader_toolbar.show()
        # Add the reader toolbar to the activity toolbox
        self.toolbox.add_toolbar('Book Viewer', self.reader_toolbar)

        #
        # Custom bookshelf toolbar
        #
        self.bookshelf_toolbar = gtk.Toolbar()
        # Save a book in the bookshelf
        self.save_button = ToolButton('document-save')
        self.save_button.set_tooltip('Save the current book in the bookshelf')
        self.save_button.connect('clicked', self.show_bookshelf)
        # Add the button to the toolbar
        self.bookshelf_toolbar.insert(self.save_button, -1)
        self.save_button.show()

        # Remove a book from the bookshelf
        #self.remove_button = ToolButton('dialog-cancel')
        #self.remove_button.set_tooltip('Remove the current book from the bookshelf')
        #self.remove_button.connect('clicked', self.iabv.remove_from_bookshelf)
        # Add the button to the toolbar
        #self.bookshelf_toolbar.insert(self.remove_button, -1)
        #self.remove_button.show()

        # Show the bookshelf toolbar
        self.bookshelf_toolbar.show()
        # Add the toolbar to the activity toolbox
        self.toolbox.add_toolbar('Book Shelf', self.bookshelf_toolbar)

        #
        # Custom help toolbar
        #
        self.help_toolbar = gtk.Toolbar()
        self.help_button = ToolButton('help') 
        self.help_button.set_tooltip('Get help')
        self.help_button.connect('clicked', iabv.help)
        # Add the button to the toolbar
        self.help_toolbar.insert(self.help_button, -1)
        self.help_button.show()
        
        # Show the bookshelf toolbar
        self.help_toolbar.show()
        # Add the toolbar to the activity toolbox
        self.toolbox.add_toolbar('Help', self.help_toolbar)
        
        #
        # End of Sugar specific stuff(widgets)
        #

        # Create the main container
        self.main_view = gtk.VBox()

        # Remove the widget's parent
        if self.iabv.widget.parent:
            self.iabv.widget.parent.remove(self.iabv.widget)

        # Attach the widget to the mainView
        self.main_view.pack_start(self.iabv.widget)

        # Show everything
        self.iabv.widget.show()
        self.main_view.show()
        # The canvas is the main section of an activity.
        # (below the activity toolbox)
        self.set_canvas(self.main_view)
        self.show_all()

    def show_bookshelf(self, *args):
        # TODO: not working
        # the window shouldn't be show as a new window, 
        # it should be showed inside the canvas
        if self.iabv.bookshelf_window.parent:
            self.iabv.bookshelf_window.parent.remove(self.iabv.bookshelf_window)

        self.set_canvas(self.iabv.bookshelf_window)
        self.show_all()

    def read_file(self, filename):
        ''' save the activity in the Journal '''
        ''' save the current book and the current page number (or line number)'''
        # TODO: not finished
        pass

    def write_file(self, filename):
        ''' restore the activity from the Journal ''' 
        # TODO: not finished
        pass
